package com.netease.qa.web.controller;

import com.nestease.qa.dao.params.TestCaseQueryParams;
import com.netease.qa.biz.dto.TestCaseDTO;
import com.netease.qa.biz.enums.TestCaseStatus;
import com.netease.qa.biz.service.TestCaseService;
import com.netease.qa.web.utils.ServiceResult;
import com.netease.qa.web.utils.UserUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zoudongdong
 * @date 2018/3/3
 */
@Controller
public class TestCaseController {

    private static Logger log = LoggerFactory.getLogger(TestCaseController.class);

    @Resource
    private TestCaseService testCaseService;

    @RequestMapping(value = "/getNextTestCaseId", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult getNextTestCaseId() {
        log.info("******getNextTestCaseId******");

        int nextTestCaseId = testCaseService.getNextTestCaseId();
        return ServiceResult.success(nextTestCaseId);
    }

    @RequestMapping(value = "/addTestCase", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResult addTestCase(@RequestBody TestCaseDTO testCaseDTO, HttpServletRequest request) {
        log.info("******addTestCase******");

        //如果该caseId的用例已经存在,则添加失败
        boolean ifCaseIdExist = testCaseService.ifTestCaseExist(testCaseDTO.getCaseId());
        if (ifCaseIdExist) {
            return ServiceResult.successMsg("编号为" + testCaseDTO.getCaseId() + "的用例已经存在,请刷新新增页面!!!");
        }

        String currentUserId = UserUtils.getCurrentLoginUserId(request);
        //校验用户与团队以及项目的关系
        if (!UserUtils.ifUserJoinedTeam(currentUserId, testCaseDTO.getTeamId())) {
            return ServiceResult.successMsg("您不是该团队的成员,没有权限添加用例!!!");
        }
        testCaseDTO.setCreator(currentUserId);
        testCaseDTO.setLastEditor(currentUserId);
        testCaseService.addTestCase(testCaseDTO);
        return ServiceResult.successMsg("添加成功");
    }

    @RequestMapping(value = "/getAllTestCase", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult getAllTestCase() {
        log.info("******getAllTestCase******");

        List<TestCaseDTO> testCaseDTOList = testCaseService.getTestCaseInCondition(new TestCaseQueryParams());
        return ServiceResult.success(testCaseDTOList);
    }

    @RequestMapping(value = "/getTestCaseDetail", method = RequestMethod.GET)
    @ResponseBody
    public ServiceResult getTestCaseDetail(@RequestParam int caseId) {
        log.info("******getTestCaseDetail******");

        TestCaseDTO testCaseDTO = testCaseService.getTestCaseDetail(caseId);
        return ServiceResult.success(testCaseDTO);
    }

    @RequestMapping(value = "/updateTestCase", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResult updateTestCase(@RequestBody TestCaseDTO testCaseDTO, HttpServletRequest request) {
        log.info("******updateTestCase******");

        //只有是同一个团队的才有权限更新该用例
        String currentUserId = UserUtils.getCurrentLoginUserId(request);
        if (!UserUtils.ifUserJoinedTeam(currentUserId, testCaseDTO.getTeamId())) {
            return ServiceResult.successMsg("您不属于该团队,没有权限更新该用例!!!");
        }
        //如果用例非有效状态,则不能更新用例
        TestCaseDTO testCase = testCaseService.getTestCaseDetail(testCaseDTO.getCaseId());
        if (testCase.getStatus() != TestCaseStatus.Valid.getValue()) {
            return ServiceResult.successMsg("该用例已删除,不能更新用例!!!");
        }
        testCaseDTO.setLastEditor(currentUserId);
        testCaseService.updateTestCase(testCaseDTO);
        return ServiceResult.successMsg("更新成功");
    }

    @RequestMapping(value = "/deleteTestCase", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResult deleteTestCase(@RequestParam int caseId, HttpServletRequest request) {
        log.info("******deleteTestCase******");
        //查询用例详情
        TestCaseDTO testCaseDTO = testCaseService.getTestCaseDetail(caseId);

        //只有是同一个团队的才有权限删除该用例
        String currentUserId = UserUtils.getCurrentLoginUserId(request);
        if (!UserUtils.ifUserJoinedTeam(currentUserId, testCaseDTO.getTeamId())) {
            return ServiceResult.successMsg("您没有权限删除该用例!!!");
        }
        //如果用例非有效状态,则不能更新用例
        if (testCaseDTO.getStatus() != TestCaseStatus.Valid.getValue()) {
            return ServiceResult.successMsg("该用例已经删除!!!");
        }
        testCaseDTO.setLastEditor(currentUserId);
        testCaseService.deleteTestCase(testCaseDTO);
        return ServiceResult.successMsg("删除成功");
    }

    @RequestMapping(value = "/confirmUser", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResult confirmUser(@RequestParam int caseId, HttpServletRequest request) {
        log.info("******confirmUser******");

        //判断当前登录用户是否加入了 - 当前用例所属的团队
        String currentUserId = UserUtils.getCurrentLoginUserId(request);
        boolean result = testCaseService.confirmUser(caseId, currentUserId);
        if (result) {
            return ServiceResult.successMsg("验证通过");
        }
        return ServiceResult.successMsg("您不属于该团队,没有权限编辑用例");
    }
}
